package com.bootdo.wx.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.domain.SalesCouponBagsDO;
import com.bootdo.wx.domain.SalesCouponDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.SalesCouponBagsService;
import com.bootdo.wx.service.SalesCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全局优惠券
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-06-13 15:54:40
 */
 
@Controller
@RequestMapping("/wx/salesCoupon")
public class SalesCouponController {
	@Autowired
	private SalesCouponService salesCouponService;
	@Autowired
	private SalesCouponBagsService salesCouponBagsService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:salesCoupon:salesCoupon")
	String SalesCoupon(){
	    return "wx/salesCoupon/salesCoupon";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:salesCoupon:salesCoupon")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SalesCouponDO> salesCouponList = salesCouponService.list(query);
		int total = salesCouponService.count(query);
		PageUtils pageUtils = new PageUtils(salesCouponList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:salesCoupon:add")
	String add(){
	    return "wx/salesCoupon/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:salesCoupon:edit")
	String edit(@PathVariable("id") String id,Model model){
		SalesCouponDO salesCoupon = salesCouponService.get(id);
		model.addAttribute("salesCoupon", salesCoupon);
		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", "0");
		List<SalesCouponBagsDO> list = salesCouponBagsService.list(map);
		model.addAttribute("bags", list);
		List<Map<String, Object>> goodsList = goodsGoodsService.getList(map);
		model.addAttribute("goodsList", goodsList);
		return "wx/salesCoupon/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:salesCoupon:add")
	public R save( SalesCouponDO salesCoupon){
		salesCoupon.setId(UuidUtil.get32UUID());
		salesCoupon.setCreateAt(new Date());
		salesCoupon.setUpdateAt(new Date());
		salesCoupon.setHasScore(1);
		salesCoupon.setScore(new BigDecimal(0));
		salesCoupon.setType(1);
		salesCoupon.setSendNum(0);
		salesCoupon.setDelFlag(0);
		if(null == salesCoupon.getBagsId()||"".equals(salesCoupon.getBagsId())){
			return R.error("请选择大礼包");
		}
		if(salesCouponService.save(salesCoupon)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:salesCoupon:edit")
	public R update( SalesCouponDO salesCoupon){
		salesCouponService.update(salesCoupon);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:salesCoupon:remove")
	public R remove( String id){
		if(salesCouponService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:salesCoupon:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		salesCouponService.batchRemove(ids);
		return R.ok();
	}
	
	
	@GetMapping("/getCouponS")
	@ResponseBody
	public List<SalesCouponDO> getCouponS() {
		return salesCouponService.getCouponS();
	};

	
}
